{% extends "helpdesk/base.html" %}{% load i18n %}
{% block helpdesk_title %}{% trans "Ticket Listing" %}{% endblock %}
{% block helpdesk_head %}
<script type='text/javascript' language='javascript' src='{{ MEDIA_URL }}helpdesk/filter.js'></script>
<script type='text/javascript' language='javascript' src='{{ MEDIA_URL }}helpdesk/hover.js'></script>

<script type='text/javascript' language='javascript'>
$(document).ready(function() {
    
    // Enable Tabs
    $("#searchtabs").tabs({
        collapsible:true
    });
    // Hide all tabs by default
    $("#searchtabs > ul > li").removeClass().addClass("ui-corner-top ui-state-default");
    $("#searchtabs > div").addClass("ui-tabs-hide");

    $("#select_all").click(function() {
        $(".ticket_multi_select").attr('checked', true);
        return false;
    });
    $("#select_none").click(function() {
        $(".ticket_multi_select").attr('checked', false);
        return false;
    });
    $("#select_inverse").click(function() {
        $(".ticket_multi_select").each(function() {
            $(this).attr('checked', !$(this).attr('checked'));
        });
        return false;
    });
});
</script>
{% endblock %}
{% block helpdesk_body %}

{% load in_list %}

<div id="searchtabs">
    <ul>
    <li><a href='#tabfilter'>Query Options</a></li>
    {% if not from_saved_query %}
    <li><a href='#tabsave'>Save This Query</a></li>
    {% endif %}
    <li><a href='#tabload'>Load Saved Query</a></li>
    </ul>
    
    <div id='tabfilter'>
    <h3>{% trans "Change Query" %}</h3>
    <form>
        <select name='select' id='filterBuilderSelect'>
        <option value='Sort'>{% trans "Sorting" %}</option>
        <option value='Owner'>{% trans "Owner" %}</option>
        <option value='Queue'>{% trans "Queue" %}</option>
        <option value='Status'>{% trans "Status" %}</option>
        <option value='Keywords'>{% trans "Keywords" %}</option>
        {% if tags_enabled %}
        <option value='Tags'>{% trans "Tags" %}</option>
        {% endif %}
        </select>
        <input type='button' id='filterBuilderButton' value='+' />
    {% csrf_token %}</form>

    <form method='get' action='./'>
    <div class='filterBox{% if query_params.sorting %} filterBoxShow{% endif %}' id='filterBoxSort'>
    <label for='id_sort'>Sorting</label>
    <select id='id_sort' name='sort'>
        <option value='created'{% ifequal query_params.sorting "created"%} selected='selected'{% endifequal %}>
            {% trans "Created" %}
        </option>
        <option value='title'{% ifequal query_params.sorting "title"%} selected='selected'{% endifequal %}>
            {% trans "Title" %}
        </option>
        <option value='queue'{% ifequal query_params.sorting "queue"%} selected='selected'{% endifequal %}>
            {% trans "Queue" %}
        </option>
        <option value='status'{% ifequal query_params.sorting "status"%} selected='selected'{% endifequal %}>
            {% trans "Status" %}
        </option>
        <option value='priority'{% ifequal query_params.sorting "priority"%} selected='selected'{% endifequal %}>
            {% trans "Priority" %}
        </option>
        <option value='assigned_to'{% ifequal query_params.sorting "assigned_to"%} selected='selected'{% endifequal %}>
            {% trans "Owner" %}
        </option>
    </select>
    <label for='id_sortreverse'>Reverse</label>
    <input type='checkbox' name='sortreverse' id='id_sortreverse'{% if query_params.sortreverse %} checked='checked'{% endif %} />
    <p class='filterHelp'>Ordering applied to tickets</p>
    <input type='button' class='filterBuilderRemove' value='-' />
    </div>

    <div class='filterBox{% if query_params.filtering.assigned_to__id__in %} filterBoxShow{% endif %}' id='filterBoxOwner'>
    <label for='id_owners'>{% trans "Owner(s)" %}</label>
    <select id='id_owners' name='assigned_to' multiple='selected' size='5'>
        {% for u in user_choices %}
        <option value='{{ u.id }}'{% if u.id|in_list:query_params.filtering.assigned_to__id__in %} selected='selected'{% endif %}>
            {{ u.username }}{% ifequal u user %} (ME){% endifequal %}
        </option>
        {% endfor %}
    </select>
    <p class='filterHelp'>Ctrl-Click to select multiple options</p>
    <input type='button' class='filterBuilderRemove' value='-' />
    </div>

    <div class='filterBox{% if query_params.filtering.queue__id__in %} filterBoxShow{% endif %}' id='filterBoxQueue'>
    <label for='id_queues'>{% trans "Queue(s)" %}</label><select id='id_queues' name='queue' multiple='selected' size='5'>{% for q in queue_choices %}<option value='{{ q.id }}'{% if q.id|in_list:query_params.filtering.queue__id__in %} selected='selected'{% endif %}>{{ q.title }}</option>{% endfor %}</select>
    <p class='filterHelp'>Ctrl-click to select multiple options</p>
    <input type='button' class='filterBuilderRemove' value='-' />
    </div>

    <div class='filterBox{% if query_params.filtering.status__in %} filterBoxShow{% endif %}' id='filterBoxStatus'>
    <label for='id_statuses'>{% trans "Status(es)" %}</label><select id='id_statuses' name='status' multiple='selected' size='5'>{% for s in status_choices %}<option value='{{ s.0 }}'{% if s.0|in_list:query_params.filtering.status__in %} selected='selected'{% endif %}>{{ s.1 }}</option>{% endfor %}</select>
    <p class='filterHelp'>Ctrl-click to select multiple options</p>
    <input type='button' class='filterBuilderRemove' value='-' />
    </div>

{% if tags_enabled %}
<div class='filterBox{% if query_params.tags %} filterBoxShow{% endif %}' id='filterBoxTags'>
  <label for='id_tags'>{% trans "Tag(s)" %}</label><select id='id_tags' name='tags' multiple='selected' size='5'>{% for t in tag_choices %}<option value='{{t.name}}'{% if t.name|in_list:query_params.tags %} selected='selected'{% endif %}>{{ t.name }}</option>{% endfor %}</select>
<p class='filterHelp'>Ctrl-click to select multiple options</p>
<input type='button' class='filterBuilderRemove' value='-' />
</div>
{% endif %}

    <div class='filterBox{% if query %} filterBoxShow{% endif %}' id='filterBoxKeywords'>
    <label for='id_query'>{% trans "Keywords" %}</label><input type='text' name='q' value='{{ query }}' id='id_query' />
    <p class='filterHelp'>Keywords are case-insensitive, and will be looked for in the title, body and submitter fields.</p>
    <input type='button' class='filterBuilderRemove' value='-' />
    </div>
    <hr style='clear: both;' />
    <input type='submit' value='{% trans "Apply Filter" %}' />
    {% csrf_token %}</form>
    </div>
    
    {% if not from_saved_query %}
    <div class='tab' id='tabsave'>
    <h3>{% trans "Save Query" %}</h3>
    <form method='post' action='{% url helpdesk_savequery %}'>
    <input type='hidden' name='query_encoded' value='{{ urlsafe_query }}' />
    <dl>
        <dt><label for='id_title'>{% trans "Query Name" %}</label></dt>
        <dd><input type='text' name='title' id='id_title' /></dd>
        <dd class='form_help_text'>{% trans "This name appears in the drop-down list of saved queries. If you share your query, other users will see this name, so choose something clear and descriptive!" %}</dd>

        <dt><label for='id_shared'>{% trans "Shared?" %}</label></dt>
        <dd><input type='checkbox' name='shared' id='id_shared' /> {% trans "Yes, share this query with other users." %}</dd>
        <dd class='form_help_text'>{% trans "If you share this query, it will be visible by <em>all</em> other logged-in users." %}</dd>

    </dl>

    <div class='buttons'>
        <input type='submit' value='{% trans "Save Query" %}'>
    </div>

    {% csrf_token %}</form>
    </div>{% endif %}

    <div id='tabload'>
    <h3>{% trans "Use Saved Query" %}</h3>
    <form method='get' action='{% url helpdesk_list %}'>
    <p><label for='id_query_selector'>{% trans "Query" %}</label> <select name='saved_query' id='id_query_selector' />
        {% for q in user_saved_queries %}
        <option value='{{ q.id }}'>{{ q.title }}{% if q.shared %} (Shared{% ifnotequal user q.user %} by {{ q.user.username }}{% endifnotequal %}){% endif %}</option>
        {% endfor %}
    </select></p>
    <input type='submit' value='{% trans "Run Query" %}'>
    {% csrf_token %}</form>
    </div>
    
</div>







{{ search_message|safe }}
<form method='post' action='{% url helpdesk_mass_update %}'>
<table width='100%'>
<tr class='row_tablehead'><td colspan='9'>{% trans "Tickets" %}</td></tr>
<tr class='row_columnheads'><th>#</th><th>&nbsp;</th><th>{% trans "Pr" %}</th><th>{% trans "Title" %}</th><th>{% trans "Queue" %}</th><th>{% trans "Status" %}</th><th>{% trans "Created" %}</th><th>{% trans "Owner" %}</th>{% if tags_enabled %}<th>{% trans "Tags" %}</th>{% endif %}</tr>
{% if tickets %}{% for ticket in tickets.object_list %}
<tr class='row_{% cycle odd,even %} row_hover'>
<th><a href='{{ ticket.get_absolute_url }}'>{{ ticket.ticket }}</a></th>
<td><input type='checkbox' name='ticket_id' value='{{ ticket.id }}' class='ticket_multi_select' /></td>
<td>{{ ticket.get_priority_span }}</td>
<th><a href='{{ ticket.get_absolute_url }}'>{{ ticket.title }}</a></th>
<td>{{ ticket.queue }}</td>
<td>{{ ticket.get_status }}</td>
<td><span title='{{ ticket.created|date:"r" }}'>{{ ticket.created|timesince }} ago</span></td>
<td>{{ ticket.get_assigned_to }}</td>
{% if tags_enabled %}<td>{{ ticket.tags }}</td>{% endif %}
</tr>
{% endfor %}{% else %}
<tr class='row_odd'><td colspan='5'>{% trans "No Tickets Match Your Selection" %}</td></tr>
{% endif %}
</table>
<div class="pagination">
    <span class="step-links">
        {% if tickets.has_previous %}
            <a href="?{% if query_string %}{{ query_string }}&amp;{% endif %}page={{ tickets.previous_page_number }}">{% trans "Previous" %}</a>
        {% endif %}

        <span class="current">
            {% blocktrans with tickets.number as ticket_num and tickets.paginator.num_pages as num_pages %}Page {{ ticket_num }} of {{ num_pages }}.{% endblocktrans %}
        </span>

        {% if tickets.has_next %}
            <a href="?{% if query_string %}{{ query_string }}&amp;{% endif %}page={{ tickets.next_page_number }}">{% trans "Next" %}</a>
        {% endif %}
    </span>
</div>

<p><label>Select: </label> <a href='#select_all' id='select_all'>All</a> <a href='#select_none' id='select_none'>None</a> <a href='#select_inverse' id='select_inverse'>Inverse</a></p>

<p><label for='id_mass_action'>With Selected Tickets:</label> <select name='action' id='id_mass_action'><option value='take'>Take (Assign to me)</option><option value='delete'>Delete</option><optgroup label='Close'><option value='close'>Close (Don't Send E-Mail)</option><option value='close_public'>Close (Send E-Mail)</option></optgroup><optgroup label='Assign To'><option value='unassign'>Nobody (Unassign)</option>{% for u in user_choices %}<option value='assign_{{ u.id }}'>{{ u.username }}</option>{% endfor %}</optgroup></select> <input type='submit' value='Go' /></p>
{% csrf_token %}</form>
{% endblock %}
